package com.ai.mall.controller.front;

import com.ai.mall.entity.UsersEntity;
import com.ai.mall.service.OrdersService;
import com.ai.mall.utils.R;
import com.ai.mall.vo.req.OrderSubmitRequest;
import com.ai.mall.vo.res.OrderVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/checkout")
public class CheckoutController {

    @Autowired
    private OrdersService ordersService;

    // 去结算
    @GetMapping("/go")
    public Object goCheckout() {
        // 实现逻辑，返回结算页面所需数据
        return null;
    }

    // 提交订单
    @PostMapping("/submit")
    public R submitOrder(@RequestBody OrderSubmitRequest orderSubmitRequest, HttpSession session) {
        UsersEntity userInfo = (UsersEntity) session.getAttribute("user");
        if (userInfo == null) {
            return R.error("请先登录");
        }
        // 实现逻辑，提交订单
        R r = ordersService.submitOrder(orderSubmitRequest, userInfo);
        return r;
    }

    @PostMapping("/cancel/{id}")
    public R cancelOrder(@PathVariable Integer id) {
        // 取消订单逻辑
        boolean isCancelled = ordersService.cancelOrder(id);
        if (isCancelled) {
            return R.ok().put("message", "订单取消成功");
        } else {
            return R.error("订单取消失败");
        }
    }

    @GetMapping("/pendingPayment")
    public R getPendingPaymentOrders(HttpSession session) {
        UsersEntity userInfo = (UsersEntity) session.getAttribute("user");
        if (userInfo == null) {
            return R.error("请先登录");
        }
        // 获取待付款订单逻辑
        List<OrderVO> pendingPaymentOrders = ordersService.getPendingPaymentOrders(userInfo);
        return R.ok().put("data", pendingPaymentOrders);
    }

    @GetMapping("/pendingReceipt")
    public R getPendingReceiptOrders(HttpSession session) {
        UsersEntity userInfo = (UsersEntity) session.getAttribute("user");
        if (userInfo == null) {
            return R.error("请先登录");
        }
        // 获取待收货订单逻辑
        List<OrderVO> pendingReceiptOrders = ordersService.getPendingReceiptOrders(userInfo);
        return R.ok().put("data", pendingReceiptOrders);
    }

    @GetMapping("/pendingReview")
    public R getPendingReviewOrders(HttpSession session) {
        UsersEntity userInfo = (UsersEntity) session.getAttribute("user");
        if (userInfo == null) {
            return R.error("请先登录");
        }
        // 获取待评价订单逻辑
        List<OrderVO> pendingReviewOrders = ordersService.getPendingReviewOrders(userInfo);
        return R.ok().put("data", pendingReviewOrders);
    }

    @GetMapping("/allOrders")
    public R getAllOrders(HttpSession session) {
        UsersEntity userInfo = (UsersEntity) session.getAttribute("user");
        if (userInfo == null) {
            return R.error("请先登录");
        }
        // 获取待评价订单逻辑
        List<OrderVO> allOrders = ordersService.getAllOrders(userInfo);
        return R.ok().put("data", allOrders);
    }

    /**
     * 去支付
     * @param orderId
     * @param session
     * @return
     */
    @GetMapping("/pay/{orderId}")
    public R goPay(@PathVariable Integer orderId, HttpSession session) {
        UsersEntity userInfo = (UsersEntity) session.getAttribute("user");
        if (userInfo == null) {
            return R.error("请先登录");
        }
        // 去支付逻辑
        ordersService.goPay(orderId, userInfo);
        // 实现逻辑，跳转到支付页面
        return R.ok().put("message", "去支付成功");
    }

    
    @PostMapping("/confirmReceipt")
    public R confirmReceipt(@RequestParam Integer orderId, HttpSession session) {
        UsersEntity userInfo = (UsersEntity) session.getAttribute("user");
        if (userInfo == null) {
            return R.error("请先登录");
        }
        // 确认收货逻辑
        // 实现逻辑，更新订单状态
        ordersService.confirmReceipt(orderId);
        return R.ok().put("message", "确认收货成功");
    }
}
